#!/usr/bin/python
# encoding:utf-8
import sys
import os
sys.path.append(os.path.split(os.path.realpath(__file__))[0] + "/lib/")

import argparse
import service


def execute_args():
    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers()

    # 添加组
    p = subparsers.add_parser('gadd', help='add group')
    p.add_argument('group', action='store', help='group name')
    p.add_argument('-m', action='store', dest='desc', help='group description')

    # 添加
    p = subparsers.add_parser('add', help='add host')
    p.add_argument('name', action='store', help='host name')
    p.add_argument('host', action='store', help='host address')
    p.add_argument('-u', action='store', dest='username', help='username')
    p.add_argument('-p', action='store', dest='passwd', help='password')
    p.add_argument('-P', action='store', dest='port', type=int, help='port')
    p.add_argument('-g', action='store', dest='group', help='specify group')
    p.add_argument('-k', action='store', dest='key', help='key name')
    p.add_argument('-m', action='store', dest='desc', help='host description')
    p.add_argument('-t', action='store', dest='type', help='ssh or rdp')

    # 添加key
    p = subparsers.add_parser('kadd', help='add key')
    p.add_argument('name', action='store', help='key name')
    p.add_argument('path', action='store', help='key path')
    p.add_argument('-m', action='store', dest='desc', help='key description')

    # 删除主机
    p = subparsers.add_parser('del', help='delete host or group')
    p.add_argument('--name', action='store', dest='name', help='host name')
    p.add_argument('--group', action='store', dest='group', help='group name')
    p.add_argument('--key', action='store', dest='key', help='group name')

    # 修改主机
    p = subparsers.add_parser('smod', help='change host info')
    p.add_argument('name', action='store', help='host name')
    p.add_argument('--host', action='store', dest='host', help='host address')
    p.add_argument('-u', action='store', dest='username', help='change username')
    p.add_argument('-p', action='store', dest='passwd', help='change password')
    p.add_argument('-P', action='store', dest='port', type=int, help='change port')
    p.add_argument('-g', action='store', dest='group', help='change group')
    p.add_argument('-m', action='store', dest='desc', help='change host description')
    p.add_argument('-k', action='store', dest='key', help='change key')

    # 修改组
    p = subparsers.add_parser('gmod', help='change group info')
    p.add_argument('group', action='store', help='group name')
    p.add_argument('-g', action='store', dest='new_group', help='change group name')
    p.add_argument('-m', action='store', dest='desc', help='change group description')

    # ssh登录
    p = subparsers.add_parser('lg', help='ssh login')
    p.add_argument('name', action='store', help='host name')
    p.add_argument('--show', action='store_true',dest='show', default=False, help='just show login cmd')

    # 查看信息
    p = subparsers.add_parser('info', help='show host info')
    p.add_argument('name', action='store', help='host name')

    # 列表
    p = subparsers.add_parser('ls', help='list groups added')
    p.add_argument('-g', action='store', dest='group', help='show hosts in group')
    p.add_argument('-k', action='store_true', default=False,dest='key', help='show keys')
    p.add_argument('-a', action='store_true', dest='all', help='show all hosts')

    # 其他
    parser.add_argument('--version', action='version', version='%(prog)s beta')

    return parser


def execute_command(args=sys.argv[1:]):
    results = execute_args().parse_args(args)

    # 调接口
    commands = {
        'add': service.add_server,
        'gadd': service.add_group,
        'del': service.del_server,
        'smod': service.update_server,
        'gmod': service.update_group,
        'lg': service.ssh_login,
        'info': service.info,
        'ls': service.lists,
        'kadd': service.add_key,
    }

    func = commands[args[0]]
    func(**results.__dict__)


if __name__ == '__main__':
    execute_command()
